package jude.java.money.sly;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jude.java.money.beans.SlySite;
import jude.java.money.service.GlobalService;
import jude.java.money.spring.Services;
import jude.java.util.FileUtils;
import jude.java.util.Utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Sly {
	private static final Log L = LogFactory.getLog(Sly.class);
	public static boolean PERSISTENCE = true;
	public static final String LANG = "en";		//en, zh_Cn
	public static final String GOOGLE = "www.google.com";
	
	private static String DOMAINS[] = {
	};
	
	private static String GOOGLE_URLS[] = {
	};
	
	private static String DOMAINBYIP_FILE = null;//"E:\\domains.txt"
//	private static String DOMAINBYIP_FILE = "E:\\domains.txt";
	
	public static final String[][] KEYWORDS = {
//		Keywords.EN.Nike,
//		Keywords.EN.Jordan,
		
		Keywords.EN.GHD,
		Keywords.EN.MBT,
		Keywords.EN.ToryBurch,
		Keywords.EN.Other,
		Keywords.EN.Timberland,
		Keywords.EN.CHI,
		Keywords.EN.CL,
		
//		Keywords.EN.UGG,
//		Keywords.EN.Moncler,
		Keywords.EN.Coach,
	};
	
	public static boolean UPDATE_RANK = true;
	
	public static void main(String[] args) throws IOException {
		Utils.startSystem("money");
		sly();
		Utils.closeSystem();
	}
	
	static void sly() {
//		if (PERSISTENCE) {
//			Utils.startSystem();
//		}
		L.fatal("\n" + new Date());
		
		if (DOMAINBYIP_FILE != null && new File(DOMAINBYIP_FILE).exists()){
			handlerDomainByIP();
			return;
		}
		
		if (DOMAINS.length > 0) {
			UPDATE_RANK = false;
			tryingSites(Arrays.asList(DOMAINS), null);
			return;
		}
		if (GOOGLE_URLS.length != 0) {
			UPDATE_RANK = false;
			handlerGoogles(GOOGLE_URLS, null);
			return;
		}
		for (int i = 0; i < KEYWORDS.length; i++) {
			for (int j = 0; j < KEYWORDS[i].length; j++) {
				String keyword = KEYWORDS[i][j];
				L.fatal("Keyword: " + keyword);
				String[] googles = getGoogleUrls(keyword);
				handlerGoogles(googles, keyword);
				L.fatal("end of Keyword: " + keyword);
			}
		}
	}
	
	static void normalizer() {
		GlobalService service = Services.getGlobalService();
		String search = "audit>=0 and domain is null";
		int size = service.count(SlySite.class, search);
		int n = 0, pageSize = 500, pageIndex = 0;
		while (n < size) {
			List<SlySite> sites = service.query(SlySite.class, search, pageSize, pageIndex);
			n += sites.size();
			pageIndex ++;
			if (sites == null || sites.isEmpty())
				break;
			for (SlySite site : sites) {
				System.out.println(site.getUrl());
				if (new Normalizer().normalize(site)) {
					service.update(site);
				}
			}
			System.out.println(n);
		}
//		List<SlySite> sites = service.listAll(SlySite.class);
		
	}
	
	private static void handlerDomainByIP(){
		String regexp = "<li class=\"siteclose\"><a\\shref=\"http://domainsbyip\\.com/domaintoip/([^/\">]+)/\">";
		String file = "E:\\domains.txt";
		String content = FileUtils.readFile(new File(file), "iso-8859-1").toString();
		Pattern p = Pattern.compile(regexp);
		Matcher m = p.matcher(content);
		List<String> domains = new ArrayList<String>();
		int n = 0;
		while (m.find(n)) {
			domains.add(m.group(1));
			n = m.end(0);
		}
		
	}
	
	private static void handlerGoogles(String[] googles, String keyword) {
		List<String> urls = findSites(googles);
		LinkedHashSet<String> ourls = new LinkedHashSet<String>();
		ourls.addAll(urls);
		urls.clear();
		urls.addAll(ourls);
		tryingSites(urls, keyword);
	}
	
	private static String[] getGoogleUrls(String keyword) {
		String[] urls = new GoogleUrl(keyword).getUrls(1);
		return urls;
	}
	
	private static List<String> findSites(String[] urls) {
		List<String> r = new ArrayList<String>();
		for (int i = 0; i < urls.length; i++) {
			System.out.println(urls[i]);
			List<String> r2 = new FindSites(urls[i]).find();
			if (r2 != null && !r2.isEmpty()) {
				r.addAll(r2);
				System.out.println("found " + r2.size() + " sites");
//				L.debug("found " + r2.size() + " sites");
			}
		}
		return r;
	}
	
	private static void tryingSites(List<String> urls, String keyword) {
		L.warn("site total count: " + urls.size());
		for (int i=0, j=urls.size(); i < j; i++){
			String domain = urls.get(i);
			try {
				new TryingSite(i, domain, keyword).trying();
			} catch (Exception e) {
				e.printStackTrace();
				L.info(e.getClass().getSimpleName()+ "  " + domain);
			}
		}
	}
}
